package net.interpreader.client;

import java.util.ArrayList;

import net.interpreader.client.Annotation;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.ParagraphElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Interpreader implements EntryPoint {

	private VerticalPanel mainPanel = new VerticalPanel();
	private FlexTable annotationsFlexTable = new FlexTable();
	private HorizontalPanel addPanel = new HorizontalPanel();
	private HTML newAnnotationTextBox = new HTML();
	private FocusPanel newAnnotationFocusPanel = new FocusPanel();
	private Button addAnnotationButton = new Button("Add");
	private HTML formalToParaphraseTranslation = new HTML();
	private Label newAnnotationAdvice = new Label();
	private ArrayList<String> annotations = new ArrayList<String>();
	private ArrayList<String> annotationVariables = new ArrayList<String>();
	private Annotation currentAnnotation = new Annotation();

	/**
	 * Entry point method.
	 */
	public void onModuleLoad() {
		// Create table for annotation data.
		annotationsFlexTable.setText(0, 0, "Annotation");
		annotationsFlexTable.setText(0, 1, "Remove");

		// Assemble Add Annotation panel.
		newAnnotationFocusPanel.setSize("200px", "120px");
		newAnnotationTextBox.setWidth("100px");
		newAnnotationFocusPanel.add(newAnnotationTextBox);
		addPanel.add(newAnnotationFocusPanel);
		addPanel.add(addAnnotationButton);
		addPanel.add(newAnnotationAdvice);
		// Add styles to elements in the Add Annotation panel.
		newAnnotationTextBox.addStyleName("newAnnotationTextBox");
		newAnnotationAdvice.addStyleName("newAnnotationAdvice");

		// Assemble Main panel.
		mainPanel.add(annotationsFlexTable);
		mainPanel.add(addPanel);
		mainPanel.add(formalToParaphraseTranslation);

		// Associate the Main panel with the HTML host page.
		RootPanel.get("annotationList").add(mainPanel);

		// Move cursor focus to the input box.
		newAnnotationFocusPanel.setFocus(true);

		// Listen for mouse events on the Add button.
		addAnnotationButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addAnnotation();
			}
		});

		// Listen for keyboard events in the input box.
		newAnnotationFocusPanel.addKeyPressHandler(new KeyPressHandler() {

			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					addAnnotation();
					currentAnnotation.clear();
				} else {
				currentAnnotation.setAnnotation(event.getCharCode());
				newAnnotationTextBox.setHTML(currentAnnotation.getAnnotation());
				}
			}
		});

	}

	/**
	 * Add annotation to FlexTable. Executed when the user clicks the
	 * addAnnotationButton or presses enter in the newAnnotationTextBox.
	 */
	private void addAnnotation() {
		final String annotation = newAnnotationTextBox.getHTML().trim();
		newAnnotationFocusPanel.setFocus(true);

		// annotation must be a valid PLI sentence.
		if (!annotation.matches("^[∀∃∧∨⇒¬≡\\.x0-9]{1,10}$")) {
			newAnnotationAdvice.setText("Hint: '" + annotation
					+ "' is not a valid PLI sentence.");
			// newAnnotationTextBox.selectAll();
			return;
		}

		newAnnotationTextBox.setHTML("");

		// Add the annotation to the table.
		int row = annotationsFlexTable.getRowCount();
		annotations.add(annotation);
		annotationsFlexTable.setText(row, 0, annotation);

		// Add a button to remove this annotation from the table.
		Button removeStockButton = new Button("x");
		removeStockButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int removedIndex = annotations.indexOf(annotation);
				annotations.remove(removedIndex);
				annotationsFlexTable.removeRow(removedIndex + 1);
			}
		});
		annotationsFlexTable.setWidget(row, 1, removeStockButton);

	}
}